// pages/my/mycomment/mycomment.js
import { axios } from '../../../utils/ajax.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        totalpage: '',
        userid: '',
        token: '',
        titleindex: 0,
        canload: false,
        //我的评价数据
        data: '',
        //我的赞数据
        zandata: '',
        //评价按钮显示隐藏
        canpublish: true,
        emptyComment: false,
        emptyLike: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let { userid = '', token = '' } = wx.getStorageSync('userinfo')
        this.setData({
            userid: userid,
            token: token
        })
        this.getdata()


        //查看有无待评价商品
        if (userid) {
            axios({
                url: '/bask/baskgoodslist',
                data: {
                    userid: userid,
                    token: token,
                    goods_id: '',
                    page: 1
                }
            }).then((res) => {
                console.log(res)
                this.setData({
                    canpublish: res.data.availableCount ? true : false
                })
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let page = this.data.page
        if (page < this.data.totalpage) {
            page++
            this.setData({
                page: page
            })
            this.getdata(true)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //发布评价
    publish() {
        wx.navigateTo({
            url: '../../home/goodsDetail/appraise/publish/publish',
        })
    },
    //导航切换
    choosenav(e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            titleindex: index,
            page: 1,
            canload: false
        })
        this.getdata()
    },
    //去商品详情
    gogooddetail(e) {
        let goodsid = e.currentTarget.dataset.goodsid
        let goodsname = e.currentTarget.dataset.goodsname
        console.log(goodsid, goodsname)
        wx.navigateTo({
            url: `../../home/goodsDetail/goodsDetail?linkId=${goodsid}`,
        })
    },
    //查看缩略图
    openimg(e) {
        let index = e.currentTarget.dataset.index
        let thumb = e.currentTarget.dataset.thumb
        let obj = this.data.titleindex == 0 ? this.data.data : this.data.zandata
        let result = obj[index].photos
        let previewImage = []
        previewImage = result.map(x => x.thumbUrl)
        wx.previewImage({
            current: thumb,
            urls: previewImage,
        })
    },
    //获取数据
    getdata(loadmore = false) {
        //我的评价
        if (this.data.titleindex == 0) {
            axios({
                url: '/Bask/userList',
                data: {
                    userid: this.data.userid,
                    token: this.data.token,
                    index: this.data.page
                }
            }).then((res) => {
                //加载更多
                if (loadmore) {
                    this.setData({
                        data: this.data.data.concat(res.data.data.list)
                    })
                } else {
                    // console.log('我的评价：', res.data.data.list)
                    this.setData({
                        data: res.data.data.list,
                        emptyComment: res.data.data.list.length > 0 ? false : true
                    })
                }
                this.setData({
                    totalpage: Math.ceil(res.data.data.totals / 10)
                })
                if (this.data.page >= this.data.totalpage) {
                    this.setData({
                        canload: false
                    })
                } else {
                    this.setData({
                        canload: true
                    })
                }
            })
            //我的赞
        } else {
            axios({
                url: "/Bask/userLikeList",
                data: {
                    userid: this.data.userid,
                    token: this.data.token,
                    index: this.data.page
                }
            }).then((res) => {
                console.log(res)
                //加载更多
                if (loadmore) {
                    this.setData({
                        zandata: this.data.zandata.concat(res.data.list)
                    })
                } else {
                    this.setData({
                        zandata: res.data.list,
                        emptyLike: res.data.list.length > 0 ? false : true
                    })
                }
                this.setData({
                    totalpage: Math.ceil(res.data.totals / 10)
                })
                if (this.data.page >= this.data.totalpage) {
                    this.setData({
                        canload: false
                    })
                } else {
                    this.setData({
                        canload: true
                    })
                }
            })
        }
    },
    //点赞/取消赞
    givelike(e) {
        let islike = e.currentTarget.dataset.islike
        let boid = e.currentTarget.dataset.boid
        let like = e.currentTarget.dataset.like
        console.log(boid, islike, like)
        axios({
            url: '/bask/like',
            data: {
                userid: this.data.userid,
                token: this.data.token,
                boId: boid,
                type: islike == 1 ? 0 : 1
            }
        }).then((res) => {
            console.log(res)
            let newdata = this.data.titleindex == 0 ? this.data.data : this.data.zandata
            newdata.find(x => x.boId == boid).isLike = !newdata.find(x => x.boId == boid).isLike
            if (islike == 1) {
                newdata.find(x => x.boId == boid).like = parseInt(newdata.find(x => x.boId == boid).like) - 1
            } else {
                newdata.find(x => x.boId == boid).like = parseInt(newdata.find(x => x.boId == boid).like) + 1
            }
            //我的评价
            if (this.data.titleindex == 0) {
                this.setData({
                    data: newdata
                })
            } else {
                this.setData({
                    zandata: newdata
                })
            }
        })
    }
})